package StudentProject.dao;

import StudentProject.bean.User;
import StudentProject.jdbc.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao {

    private static final String SQL_USER_LOGING = "SELECT type from user WHERE uname = ? AND password = ?";
    private static final String SQL_USER_INSET = "INSERT INTO user VALUES(userid,?,?,2)";
    private static final String SQL_USER_DELETE = "DELETE from user where uname = ?";
    private static final String SQL_USER_UPDAE = "UPDATE user set password = ? where uname = ?";
    private static final String SQL_USER_SELECT = "SELECT * FROM user WHERE uname = ?";

    //登录方法
    @Override
    public int login(User user) {
        //连接数据库,创建连接对象conn
        Connection conn = JDBCUtils.getConnection();
        //创建编译环境
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = conn.prepareStatement(SQL_USER_LOGING);
            //设置sql语句
            preparedStatement.setString(1, user.getUname());
            preparedStatement.setString(2, user.getPassword());
            //运行sql语句
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                //用结果集查询当前用户权限
                int type = resultSet.getInt("type");
                return type;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn, preparedStatement, resultSet);
        }

        return 0;
    }

    //添加用户(添加的默认默认权限是2)
    @Override
    public boolean insert(User user) {
        //连接数据库,创建连接对象conn
        Connection conn = JDBCUtils.getConnection();
        //创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(SQL_USER_INSET);
            //设置sql语句中的参数
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUname());
            //运行sql
            int line = preparedStatement.executeUpdate();
            //判断结果返回ture或false
            return line > 0 ? true : false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    //删除方法学生信息
    @Override
    public boolean delete(String uname) {
        //连接数据库,创建连接对象conn
        Connection conn = JDBCUtils.getConnection();
        //创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(SQL_USER_DELETE);
            //这种sql语句参数
            preparedStatement.setString(1, uname);
            //运行sql
            int line = preparedStatement.executeUpdate();
            return line > 0 ? true : false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }


    //修改学生密码
    @Override
    public boolean update(User user) {
        //连接数据库，创建连接对象conn
        Connection conn = JDBCUtils.getConnection();
        //创建预编译环境
        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = conn.prepareStatement(SQL_USER_UPDAE);
            //这种sql语句参数
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUname());
            //运行sql
            int line = preparedStatement.executeUpdate();
            return line > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }


    //查询单个学生信息
    @Override
    public User select(String uname) {
        //连接数据库，创建conn对象
        Connection conn = JDBCUtils.getConnection();
        //创建预编译环境
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(SQL_USER_SELECT);
            //设置sql参数
            preparedStatement.setString(1, uname);
            //运行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //循环结果集合
            while (resultSet.next()) {
                int userid = resultSet.getInt("userid");
                uname = resultSet.getString("uname");
                String password = resultSet.getString("password");
                int type = resultSet.getInt("type");
                return new User(userid, uname, password, type);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

